'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 秘镜
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../consts/code');
var copyRecordModel = require('../../../models/copyRecordModel');
var copyData = require('../../../libs/war/copyData');
var atkData = require('../../../libs/war/atkData');
var copyConf = require('../../../config/copyConf');
var PlayerManager = require('../../../libs/playerManager');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 进入副本协议
 * 要给客户端下发的数据:
 * 1.敌方的建筑
 * 2.敌方的英雄,小兵
 * 3.我方的英雄,小兵
 */
Handler.prototype.enter = function (msg, session, next) {
    var role_id = session.uid;
    var copy_id = msg.copy_id;
    if (!copy_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var atk_data;
    var def_data;
    var record_id;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'copy', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var copy_model = player_manager.copy;

                // 判断这个副本的前置副本是否已经通关
                var copy_conf = copyConf.getCopyConf(copy_id);
                if (!copy_conf) {
                    return next(null, {
                        code: Code.COPY_NOT_EXIST
                    });
                }

                // 判断前置副本是否通关
                if (!!copy_conf.need_carbon && !copy_model.getCopy(copy_conf.need_carbon)) {
                    return next(null, {
                        code: Code.COPY_NEED_PASS_ERROR
                    });
                }

                // 扣除粮食
                if (!role_model.subEnergy(copy_conf.sp_cost)) {
                    return next(null, {
                        code: Code.USER_SP_NOT_ENOUGH
                    });
                }

                // 敌方数据
                def_data = _getDef(copy_id);
                if (!def_data) {
                    return next(null, {
                        code: Code.COPY_NOT_EXIST
                    });
                }

                // 我方数据
                _getAtk(role_id, cb);
            },
            function (data, cb) {
                atk_data = data;
                copyRecordModel.addRecord(role_id, copy_id, atk_data, def_data, cb);
            },
            function (model, cb) {
                record_id = model.getRecordId();
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    record_id: record_id,
                    atk: atk_data,
                    def: def_data
                }
            });
        }
    );
};

/**
 * 获取攻击方数据
 * @return {[type]} [description]
 */
var _getAtk = function (role_id, cb) {
    atkData.getData(role_id, cb);
};

/**
 * 获取防守方数据
 * @return {[type]} [description]
 */
var _getDef = function (copy_id) {
    return copyData.getData(copy_id);
};
